﻿
using DataMatrix.net;
using System;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace COMTEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   

        }

        private float DMlabelScaling = 1.0f;
        private float QRlabelScaling = 1.0f;
        private int start_x = 40;
        private int start_y = 40;
        private int width = 128;
        private int height = 128;

        private void btn_paintcode_Click(object sender, EventArgs e)
        {
            DMlabelScaling = 1.0f;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_DMCode(g,1.0f);
        }

        private void btn_big_Click(object sender, EventArgs e)
        {
            DMlabelScaling += 0.5f;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_DMCode(g, DMlabelScaling);
        }

        private void btn_small_Click(object sender, EventArgs e)
        {
            DMlabelScaling-= 0.5f;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_DMCode(g, DMlabelScaling);
        }


        private void Refresh_DMCode(Graphics g,float Scaling)
        {
            this.panel1.Controls.Clear();
            this.panel1.Refresh();

            DmtxImageEncoder Die = new DmtxImageEncoder();
            DataMatrix.net.DmtxImageEncoderOptions option = new DmtxImageEncoderOptions();
            option.SizeIdx = DmtxSymbolSize.DmtxSymbol32x32;//形状
            option.MarginSize = 0;//边距
            option.ModuleSize = 2;//点阵大小

            bool[,] datas = Die.EncodeRawData("W1234567890.09877654321##$340000PP.END", option);

            SolidBrush mybrush = new SolidBrush(Color.Black);

            int row_count = datas.GetLength(0); // 行数
            int col_count = datas.GetLength(1); // 列数

            // 计算每个像素块大小
            int pix_width = (int)((width / col_count)* Scaling);
            int pix_height = (int)((height / row_count)* Scaling);

            for (int i = 0; i < row_count; i++)
            {
                for (int j = 0; j < col_count; j++)
                {
                    // 画二维码，纵向
                    // 计算起始XY
                    int x = i * pix_width;
                    int y = j * pix_height;
                    if (datas[i, j] == true)
                    {
                        g.FillRectangle(mybrush, start_x + x, start_y + y, pix_width, pix_height);
                    }
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            QRlabelScaling = 1.0F;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_QRCode(g, QRlabelScaling);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            QRlabelScaling += 0.5F;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_QRCode(g, QRlabelScaling);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            QRlabelScaling-= 0.5F;
            Graphics g = this.panel1.CreateGraphics();
            Refresh_QRCode(g, QRlabelScaling);
        }

        private void Refresh_QRCode(Graphics g, float Scaling)
        {
            this.panel1.Controls.Clear();
            this.panel1.Refresh();

            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;

            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = (int)(width* Scaling),
                Height = (int)(height * Scaling),
                Margin = 0,
                QrVersion = 2,
            };

            options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;

            writer.Options = options;

            BitMatrix bm = writer.Encode("1234567899999");

            for (int i = 0; i < bm.Height; i++)
            {
                BitArray ba = bm.getRow(i, null);

                for (int j = 0; j < ba.Size; j++)
                {
                    //datas[i, j] = bm[i, j];
                    if (ba[j])
                    {
                        //rects[bm.Height * i + j] = new Rectangle(j + RelativeY, i + RelativeX, 1, 1);
                        g.FillRectangle(Brushes.Black, j + start_x, i + start_y, 1, 1);
                    }
                }
            }
        }

    }
}
